import 'package:changxindianzishangwu/dao/user_info_dao.dart';
import 'package:changxindianzishangwu/model/base_model.dart';
import 'package:changxindianzishangwu/pages/login/country_phone_page.dart';
import 'package:changxindianzishangwu/pages/my/settings/bind_phone_code_page.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/app_string.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/util/universal_view.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:changxindianzishangwu/widget/icon_text_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

///绑定手机号=》输入手机号
class BindPhonePage extends StatefulWidget {
  const BindPhonePage({Key key}) : super(key: key);

  @override
  _BindPhonePageState createState() => _BindPhonePageState();
}

class _BindPhonePageState extends State<BindPhonePage> {
  String phoneCountry = "+86";
  TextEditingController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = TextEditingController();
  }

  @override
  void dispose() {
    _controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(context, "绑定手机号"),
      body: GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          color: Colors.white,
          padding: EdgeInsets.all(width(15)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                AppString.bindPhoneHint,
                style: _styleTv(15, AppColor.colorDefaultText),
              ),
              SizedBox(
                height: height(30),
              ),
              Text(
                "手机号码",
                style: _styleTv(13, AppColor.colorText85),
              ),
              Row(
                children: [
                  IconTextButton.icon(
                    onPressed: () {
                      Get.to(() => CountryPhonePage()).then((value) {
                        setState(() {
                          phoneCountry = value != null ? value : phoneCountry;
                        });
                      });
                    },
                    icon: Icon(
                      Icons.keyboard_arrow_down_outlined,
                      color: AppColor.colorDefaultText,
                    ),
                    label: Text(phoneCountry,
                        style: _styleTv(15, AppColor.colorDefaultText)),
                    iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                    elevation: 0,
                  ),
                  Expanded(
                    flex: 1,
                    child: TextField(
                      controller: _controller,
                      maxLines: 1,
                      keyboardType: TextInputType.phone,
                      cursorColor: Colors.greenAccent,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: "请输入手机号码",
                        hintStyle: TextStyle(
                          fontSize: sp(13),
                          color: Color.fromRGBO(153, 153, 153, 1),
                        ),
                      ),
                      inputFormatters: [
                        LengthLimitingTextInputFormatter(11), //最大长度
                      ],
                    ),
                  )
                ],
              ),
              UniversalView().line(),
              SizedBox(
                height: height(60),
              ),
              UniversalView().longBtn("下一步", () {
                if (_controller.text.isEmpty || _controller.text.length < 11) {
                  return toast("请输入手机号");
                }

                _checkPhone();
              })
            ],
          ),
        ),
      ),
    );
  }

  TextStyle _styleTv(double size, color) {
    return TextStyle(fontSize: sp(size), color: color);
  }

  ///检查手机号是否使用过
  void _checkPhone() async{
    BaseModel _baseModel =await UserInfoDao.checkUserPhone(_controller.text);
    if (_baseModel.resultCode != 1) {
      return toast("${_baseModel.resultMsg}");
    }
    print('-BindphonePage--phoneCountry-----${phoneCountry}');
    phoneCountry=phoneCountry.substring(1,phoneCountry.length);
    Get.to(BindphoneCodePage(
            phone: _controller.text, phoneCountry: phoneCountry))
        .then((value) {
      if (value) {
        Get.back(result: _controller.text);
      }
    });
  }
}
